class Solution:
    def romanToInt(self, s: str) -> int:
        query = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
        spe_query = {'IV':4, 'IX':9, 'XL':40, 'XC':90, 'CD':400, 'CM':900}

        i_before = ''
        sum = 0
        for i in s:  
            s_two = i_before + i 
            if s_two in spe_query:
                sum = sum + spe_query[s_two] - query[i_before]
            else:
                sum += query[i]
            i_before = i 
        return sum

class Solution:
    def romanToInt(self, s: str) -> int:
        query = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
        sum = query[s[0]]
        length = len(s)
        for i in range(1, length):
            if query[s[i-1]] < query[s[i]]:
                sum = sum + query[s[i]] - query[s[i-1]] * 2
            else:
                sum += query[s[i]]
        return sum